1
|
|
|
const cheerio = require('cheerio') |
2
|
|
|
const fs = require('fs') |
3
|
|
|
const { join } = require('path') |
4
|
|
|
|
5
|
|
|
const ruleA = require('../../rules/a') |
6
|
|
|
const ruleH1 = require('../../rules/h1') |
7
|
|
|
const ruleHead = require('../../rules/head') |
8
|
|
|
const ruleImg = require('../../rules/img') |
9
|
|
|
const ruleStrong = require('../../rules/strong') |
10
|
|
|
|
11
|
|
|
const path = join(__dirname, '../sample.html') |
12
|
|
|
const html = fs.readFileSync(path, 'utf8') |
13
|
|
|
const sampleData = cheerio.load(html, { |
14
|
|
|
normalizeWhitespace: true |
15
|
|
|
}) |
16
|
|
|
|
17
|
|
|
describe('test Rules set', () => { |
18
|
|
|
it('rule a should fail', () => { |
19
|
|
|
const result = ruleA(sampleData) |
20
|
|
|
expect(result).toBe('There are 1 <a> tag without rel attribute') |
21
|
|
|
}) |
22
|
|
|
|
23
|
|
|
it('rule h1 should fail', () => { |
24
|
|
|
const result = ruleH1(sampleData) |
25
|
|
|
expect(result).toBe('This HTML has more than 1 <h1> tag') |
26
|
|
|
}) |
27
|
|
|
|
28
|
|
|
it('rule img should fail', () => { |
29
|
|
|
const result = ruleImg(sampleData) |
30
|
|
|
expect(result).toBe('There are 2 <img> tag without alt attribute') |
31
|
|
|
}) |
32
|
|
|
|
33
|
|
|
it('rule strong should fail', () => { |
34
|
|
|
const options = { rules: { strong: { threadhold: 2 } } } |
35
|
|
|
const result = ruleStrong(sampleData, options) |
36
|
|
|
expect(result).toBe('This HTML has more than 2 <strong> tag') |
37
|
|
|
}) |
38
|
|
|
|
39
|
|
|
it('rule head should fail', () => { |
40
|
|
|
const failHead = cheerio.load('<html><head></head></html>', {}) |
41
|
|
|
const result = ruleHead(failHead) |
42
|
|
|
expect(result).toBe( |
43
|
|
|
`header that doesn’t have <title> tag\r\nheader that doesn’t have descriptions <meta> tag\r\nheader that doesn’t have keywords <meta> tag` |
44
|
|
|
) |
45
|
|
|
}) |
46
|
|
|
|
47
|
|
|
it('rule a should success', () => { |
48
|
|
|
const successData = cheerio.load('<body><a rel="x"></a></body>', {}) |
49
|
|
|
const result = ruleA(successData) |
50
|
|
|
expect(result).toBeNull() |
51
|
|
|
}) |
52
|
|
|
|
53
|
|
|
it('rule h1 should fail', () => { |
54
|
|
|
const successData = cheerio.load('<body><h1></h1></body>', {}) |
55
|
|
|
const result = ruleH1(successData) |
56
|
|
|
expect(result).toBeNull() |
57
|
|
|
}) |
58
|
|
|
|
59
|
|
|
it('rule img should fail', () => { |
60
|
|
|
const successData = cheerio.load('<body><img alt="x"></img></body>', {}) |
61
|
|
|
const result = ruleImg(successData) |
62
|
|
|
expect(result).toBeNull() |
63
|
|
|
}) |
64
|
|
|
|
65
|
|
|
it('rule strong should fail', () => { |
66
|
|
|
const successData = cheerio.load('<html><strong>strong</strong></html>', { |
67
|
|
|
normalizeWhitespace: true |
68
|
|
|
}) |
69
|
|
|
const options = { rules: { strong: { threadhold: 2 } } } |
70
|
|
|
const result = ruleStrong(successData, options) |
71
|
|
|
expect(result).toBeNull() |
72
|
|
|
}) |
73
|
|
|
|
74
|
|
|
it('rule head should fail', () => { |
75
|
|
|
const result = ruleHead(sampleData) |
76
|
|
|
expect(result).toBeNull() |
77
|
|
|
}) |
78
|
|
|
}) |
79
|
|
|
|